package com.wang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.mapper.ComplaintMapper;
import com.wang.entity.Complaint;
import com.wang.service.ComplaintService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
* @author 33115
* @description 针对表【complaint】的数据库操作Service实现
* @createDate 2024-06-17 11:12:44
*/
@Service
public class ComplaintServiceImpl extends ServiceImpl<ComplaintMapper, Complaint>
    implements ComplaintService{
    @Override
    public List <Complaint> getComplaint(String content, LocalDateTime startDate, LocalDateTime endDate) {
        LambdaQueryWrapper<Complaint> queryWrapper = new LambdaQueryWrapper<>();
        System.out.println(StringUtils.isNotBlank(content));
        // 分别检查并设置查询条件
        if (StringUtils.isNotBlank(content)) {
            queryWrapper.like(Complaint::getContent, "%" + content + "%"); // 根据标题模糊匹配
        }

        if (startDate != null && endDate != null) {
            queryWrapper.between(Complaint::getHandleTime, startDate, endDate); // 根据发布时间筛选
        }

        // 确保至少有一个查询条件被设置
        if (queryWrapper.isEmptyOfWhere()) {
            throw new IllegalArgumentException("至少需要提供标题关键词或时间范围进行查询");
        }

        return list(queryWrapper);

    }
    @Override
    public void updateComplaint(Complaint complaint) {
        updateById(complaint); // 使用MyBatis-Plus的updateById方法直接更新

    }
}




